package com.tencent.sr.iris.activity.service.activity.impl;

import cn.hutool.core.util.StrUtil;
import com.tencent.sr.iris.activity.common.enums.TaskRecordAwardIssueStatusEnum;
import com.tencent.sr.iris.activity.interfaces.enums.ActivityIdEnum;
import com.tencent.sr.iris.activity.interfaces.request.acticity.BatchDataRequest;
import com.tencent.sr.iris.activity.interfaces.response.activity.BatchStatisticsResponse;
import com.tencent.sr.iris.activity.repository.singlemapper.example.TIrisUserTaskBatchDataExample;
import com.tencent.sr.iris.activity.service.activity.BatchDataStatisticsHandler;
import com.tencent.sr.iris.activity.service.activity.EsDataOperateService;
import com.tencent.sr.iris.activity.service.util.UserTaskBatchDataUtils;
import com.tencent.sr.rmall.material.interfaces.enums.EYesNo;
import org.elasticsearch.index.query.BoolQueryBuilder;
import org.elasticsearch.index.query.QueryBuilders;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import java.util.Arrays;
import java.util.List;

@Component
public class RegisterGiftStatisticsHandler extends EsDataOperateService
        implements BatchDataStatisticsHandler {

    @Value("${env.es.batch.data.index.name:user-task-batch-data}")
    private String indexName;

    @Override
    protected String[] indices() {
        return new String[]{indexName};
    }

    @Override
    public BatchStatisticsResponse statistics(BatchDataRequest request) {

        //券发放数
        int totalCouponCount = couponCount(request, null);

        //特邀券发放数
        int totalSpecialCouponCount = couponCount(request, Integer.valueOf(EYesNo.YES.getValue()));

        //券核销数
        int couponConsumeCount = couponConsumeCount(request, null);

        //特邀券核销数
        int specialCouponConsumeCount = couponConsumeCount(request, Integer.valueOf(EYesNo.YES.getValue()));

        return new BatchStatisticsResponse()
                .setCouponConsumeCount(couponConsumeCount)
                .setSpecialCouponConsumeCount(specialCouponConsumeCount)
                .setTotalCouponCount(totalCouponCount)
                .setTotalSpecialCouponCount(totalSpecialCouponCount);
    }


    @Override
    public List<String> supportedActivityIds() {
        return Arrays.asList(ActivityIdEnum.REGIST_GIFT.getCode());
    }

    /**
     * 券发放数
     *
     * @param request
     * @param special
     * @return
     */
    private int couponCount(BatchDataRequest request, Integer special) {
        BoolQueryBuilder query = QueryBuilders.boolQuery()
                .must(QueryBuilders.termQuery(StrUtil.toCamelCase(TIrisUserTaskBatchDataExample.AWARD_ISSUE_STATUS),
                        TaskRecordAwardIssueStatusEnum.ISSUE_SUCCEED.getCode()));
        if (special != null) {
            query.must(QueryBuilders
                    .termQuery(StrUtil.toCamelCase(TIrisUserTaskBatchDataExample.UID_SPECIAL_INVITE), special));
        }
        UserTaskBatchDataUtils.buildCondition(query, request);
        return (int) esCount(query, "0");
    }

    /**
     * 券核销数
     *
     * @param request
     * @param special
     * @return
     */
    private int couponConsumeCount(BatchDataRequest request, Integer special) {
        BoolQueryBuilder query = QueryBuilders.boolQuery()
                .must(QueryBuilders.termQuery(StrUtil.toCamelCase(TIrisUserTaskBatchDataExample.AWARD_ISSUE_STATUS),
                        TaskRecordAwardIssueStatusEnum.ISSUE_SUCCEED.getCode()))
                .must(QueryBuilders.existsQuery(StrUtil.toCamelCase(TIrisUserTaskBatchDataExample.CONSUME_TIME)));
        if (special != null) {
            query.must(QueryBuilders.termQuery(StrUtil.toCamelCase(TIrisUserTaskBatchDataExample.UID_SPECIAL_INVITE),
                    special));
        }

        UserTaskBatchDataUtils.buildCondition(query, request);
        return (int) esCount(query, "0");
    }
}
